/** 主界面 */
cc.Class({
    extends: cc.Component,

    properties: {
        /** 城镇地图 */
        sp_map:cc.Sprite,
        /** 建筑 */
        sp_area:[cc.Sprite],
        /** 新建筑预制件 */
        pf_newbuild:cc.Prefab,
        /** 英雄头像 */
        sp_photo:cc.Sprite,
        /** 声音按钮 */
        bt_sound:cc.Button,
        /** 帮助按钮 */
        bt_help:cc.Button,
        /** 收藏按钮 */
        bt_fav:cc.Button,
        /** 退出按钮 */
        bt_exit:cc.Button,
        /** 显示基本信息 */
        txt_baseshow:[cc.Label],
        /** 英雄列表 */
        nd_hero:cc.Node,
        /** 城镇列表 */
        nd_city:cc.Node,
        /** 商城按钮 */
        bt_shop:cc.Button,
        /** 充值按钮 */
        bt_pay:cc.Button,
        /** 排行按钮 */
        bt_rank:cc.Button,
        /** 任务按钮 */
        bt_task:cc.Button,
        /** 工会按钮 */
        bt_church:cc.Button,
        /** 信息按钮 */
        bt_mail:cc.Button,
        /** 城镇按钮 */
        nd_citybt:cc.Node,
        /** 英雄按钮 */
        nd_herobt:cc.Node,
        /** 建筑窗口 */
        pf_build:[cc.Prefab],
        /** 打开的窗口 */
        _preview:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //cc.view.setResolutionPolicy(cc.ResolutionPolicy.EXACT_FIT);
        if(!(App.U && App.U.uid > 0)){
            return cc.director.loadScene("loading");
        }

        for(let i=1;i<5;i++){
            let nd = this.nd_citybt.getChildByName("bt_" + i);
            nd.on(cc.Node.EventType.TOUCH_END, this.onCityBt, this);
        }
        for(let i=1;i<8;i++){
            let nd = this.nd_herobt.getChildByName("bt_" + i);
            nd.on(cc.Node.EventType.TOUCH_END, this.onHeroBt, this);
        }
        this.schedule(function(){
            let ct = App.U.city[App.cid];
            for(let i=0;i<ct.area.length;i++){
                if(ct.area[i]){
                    if(ct.area[i].otime > 0){ //建筑正在升级
                        if(Date.now() - ct.area[i].otime - App.dtm > 0){
                            App.net.send([2,15,{aid:i+1, cid:App.cid}]);
                            ct.area[i].otime = 0;
                            break;
                        }                        
                    }else if(ct.area[i].endtime > 0){ //研究正在研究
                        if(Date.now() - ct.area[i].endtime - App.dtm > 0){
                            App.net.send([2,46,{aid:i+1, cid:App.cid}]);
                            break;
                        } 
                    }
                    //cc.log(i, Date.now(), ct.area[i].otime, App.dtm);

                }
            }
            //cc.log('cc', Date.now(), ct.area.length);
        }, 1);

        App.enterGame(this);
    },
    
    start () {
        if(!(App.U && App.U.uid > 0)){
            return;
        }
        //显示资源
        this.updateRes();
        //显示英雄列表
        for(let i=0;i<App.U.hero.length;i++){
            let nd = new cc.Node(i.toString()), hr = App.U.hero[i];
            let spr = nd.addComponent(cc.Sprite);
            spr.spriteFrame = new cc.SpriteFrame(App.path + "hero/m_" + hr.race + "_1.jpg");
            nd.scale = 0.47;
            nd.anchorY = 1;
            nd.parent = this.nd_hero;
            nd.on(cc.Node.EventType.TOUCH_END, this.onClickHero, this);
        }
        //显示城镇列表
        for(let i=0;i<App.U.city.length;i++){
            let nd = new cc.Node(i.toString()), ct = App.U.city[i];
            let spr = nd.addComponent(cc.Sprite);
            spr.spriteFrame = new cc.SpriteFrame(App.path + "city/city" + ct.race + ".jpg");
            nd.scale = 0.47;
            nd.anchorY = 1;
            nd.parent = this.nd_city;
            nd.on(cc.Node.EventType.TOUCH_END, this.onClickCity, this);
        }
        this.setCity(0);
        //this.setHero(0);
    },
    /** 点击英雄 */
    onClickHero(ev){
        let ind = this.nd_hero.children.indexOf(ev.target);
        this.setHero(ind);
    },
    /** 点击城镇 */
    onClickCity(ev){
        let ind = this.nd_city.children.indexOf(ev.target);
        this.setCity(ind);
    },
    /** 城镇按钮 */
    onCityBt(ev){
        cc.log("citybt",ev.target.name);
        if(this._preview){
            this._preview.parent = null;
            this._preview = null;
        } 
        if(ev.target.name == "bt_1"){ //建筑队列
            this._preview = cc.instantiate(this.pf_build[14]);
        }else if(ev.target.name == "bt_2"){ //部队交换

        }else if(ev.target.name == "bt_3"){ //城镇驻守

        }else if(ev.target.name == "bt_4"){ //外城关卡

        }
        this._preview.parent = this.node;
    },    
    /** 英雄按钮 */
    onHeroBt(ev){
        cc.log("herobt",ev.target.name);
        if(ev.target.name == "bt_1"){ //属性

        }else if(ev.target.name == "bt_2"){ //背包

        }else if(ev.target.name == "bt_3"){ //魔法

        }else if(ev.target.name == "bt_4"){ //技能

        }else if(ev.target.name == "bt_5"){ //贩卖

        }else if(ev.target.name == "bt_6"){ //传输

        }else if(ev.target.name == "bt_7"){ //荣誉

        }
    },  
    /** 设置城镇id */
    setCity(cid){
        App.cid = cid;
        let ct = App.U.city[cid];
        if(!ct){
            return cc.log("city error", cid);
        }
        this.nd_citybt.active = true;
        this.nd_herobt.active = false;

        let race = "r"+ct.race+"_";
        this.sp_map.spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + "back.jpg");
        for(let i=0;i<ct.area.length;i++){
            if(!ct.area[i]) continue;
            this.sp_area[i].spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + App.C.build[ct.area[i].bid].list[ct.area[i].lev-1][3] + ".png");
        }
        this.sp_photo.spriteFrame = this.nd_city.children[cid].getComponent(cc.Sprite).spriteFrame;
        this.txt_baseshow[0].string = "城镇名：" + ct.cityname;
        this.txt_baseshow[1].string = "坐标：" + ct.m_x + ":" + ct.m_y;
        this.txt_baseshow[2].string = "居民/空闲：" + ct.worker + ":" + ct.feelworker;
        this.txt_baseshow[3].string = "繁衍值：" + ct.breednum;
        for(let i=0;i<20;i++){
            this.sp_area[i].node.on(cc.Node.EventType.TOUCH_END, function(ev){
                if(this._preview){
                    this._preview.parent = null;
                    this._preview = null;
                } 
                let aid = ev.target.name.substring(4)|0;
                //cc.log("aid", aid);
                if(App.U.city[App.cid].area[aid - 1]){
                    //cc.log("aaa", App.U.city[App.cid].area[aid - 1]);
                    if(aid > 11){
                        if(App.U.city[App.cid].area[aid - 1].bid == 21){
                            this._preview = cc.instantiate(this.pf_build[11]);
                        }else{
                            this._preview = cc.instantiate(this.pf_build[9]);
                        }
                    }else{
                        this._preview = cc.instantiate(this.pf_build[aid - 1]);
                    }
                    let com = this._preview.getComponent(cc.Component);
                    if(com && com.setAid) com.setAid(aid);
                    this._preview.parent = this.node;
                }else{
                    this._preview = cc.instantiate(this.pf_newbuild);
                    this._preview.parent = this.node;
                    let com = this._preview.getComponent(cc.Component);
                    com.setAid(aid);
                }
            }, this);
        }
    },
    /** 设置当前英雄id */
    setHero(hid){
        App.hid = hid;
        let hr = App.U.hero[hid];
        if(!hr){
            return cc.log("hero error", hid);
        }
        this.nd_herobt.active = true;
        this.nd_citybt.active = false;

        this.sp_photo.spriteFrame = this.nd_hero.children[hid].getComponent(cc.Sprite).spriteFrame;
        this.txt_baseshow[0].string = "英雄名：" + hr.heroname;
        this.txt_baseshow[1].string = "地图名：" + hr.map[4];
        this.txt_baseshow[2].string = "坐标：" + hr.map[2] + ":" + hr.map[3];
        this.txt_baseshow[3].string = "行动力：" + (hr.heromove-hr.usemove) + "/" + hr.heromove;
    },
    /** 接受到网络消息 */
    onNetData(msg){
        if(msg[0] == 2){ //城镇相关消息
            switch (msg[1]) {
                case 113:{ //新建建筑返回
                    let bd = App.C.build[msg[2].bid].list[0];
                    for(let i=0;i<App.resKey.length-1;i++){
                        App.U.res[App.resKey[i]] -= bd[i+5];
                    }
                    App.U.res.addgod += bd[12];
                    App.U.city[msg[2].cid].area[msg[2].aid-1] = {bid:msg[2].bid,lev:1,otime:Date.now() + App.dtm + bd[4]*1000};
                    let ct = App.U.city[msg[2].cid];
                    this.sp_area[msg[2].aid-1].spriteFrame = new cc.SpriteFrame(App.path + "race/r"+ct.race+"_" + bd[3] + ".png");
                    if(this._preview) this._preview.parent = null;
                    this.updateRes();
                    return;
                }case 114:{ //升级建筑
                    let ct = App.U.city[msg[2].cid], bd = ct.area[msg[2].aid-1];
                    let bi = App.C.build[bd.bid].list[bd.lev];
                    for(let i=0;i<App.resKey.length-1;i++){
                        App.U.res[App.resKey[i]] -= bi[i+5];
                    }
                    App.U.res.addgod += bi[12];
                    bd.lev += 1;
                    bd.otime = Date.now() + App.dtm + bi[2]*1000;
                    this.sp_area[msg[2].aid-1].spriteFrame = new cc.SpriteFrame(App.path + "race/r"+ct.race+"_" + bi[3] + ".png");
                    if(this._preview) this._preview.parent = null;
                    this.updateRes();
                    return;
                }case 115:{ //完成一个建筑
                    App.U.city[msg[2].cid].queue--;
                    return;
                }case 145:{ //升级一个研究
                    let bd = App.U.city[msg[2].cid].area[msg[2].aid-1], lev = 0, ai = App.C.alchemy[0][msg[2].fid];
                    if(ai.list) ai = ai.list;
                    if(bd.fids[bd.sid]) lev = bd.fids[bd.sid];
                    ai = ai[lev];
                    for(let i=0;i<App.resKey.length-1;i++){
                        App.U.res[App.resKey[i]] -= ai[i];
                    }
                    bd.fid = msg[2].fid;
                    bd.sid = msg[2].sid;
                    bd.endtime = Date.now() + App.dtm + ai[7]*1000;
                    this.updateRes();
                    break;
                }case 146:{ //完成一个研究
                    let bd = App.U.city[msg[2].cid].area[msg[2].aid-1];
                    if(bd.bid == 10){
                        if(bd.fids[bd.sid]) bd.fids[bd.sid]++;
                        else bd.fids[bd.sid] = 1;
                    }else{
                        if(bd.fids[bd.fid]) bd.fids[bd.fid]++;
                        else bd.fids[bd.fid] = 1;
                    }
                    bd.fid = bd.endtime = 0;
                    bd.sid = -1;
                    break;
                }
            }
            if(this._preview){
                let com = this._preview.getComponent(cc.Component);
                if(com.onNetData) com.onNetData(msg);
            } 
        }else if(msg[0] == 3){ //部队信息
            switch(msg[1]){
                case 101:{ //部队招募完成
                    let tr = App.C.troop[msg[2].tid], ti = 0;
                    for(let i=0;i<App.resKey.length-1;i++){
                        App.U.res[App.resKey[i]] -= tr[App.resKey[i]];
                    }
                    if(App.U.city[msg[2].cid].troops[msg[2].caid]) App.U.city[msg[2].cid].troops[msg[2].caid].num += msg[2].num;
                    else App.U.city[msg[2].cid].troops[msg[2].caid] = {tid:msg[2].tid,num:msg[2].num};
                    break;
                }
            }
        }
    },
    /** 更新资源 */
    updateRes(){
        for(let k in App.U.res){
            let lb = cc.find("Canvas/top/txt_" + k).getComponent(cc.Label);
            lb.string = App.U.res[k];
        }
        let lb = cc.find("Canvas/top/txt_gwcoin").getComponent(cc.Label);
        lb.string = App.U.gwcoin;
    }
    // update (dt) {},
});
